package com.servlet.music;


import com.dao.SongService;
import com.dao.SongerService;
import com.dao.StyleService;
import com.dao.impl.SongServiceImpl;
import com.dao.impl.SongerServiceImpl;
import com.dao.impl.StyleServiceImpl;
import com.entity.Song;
import com.entity.Songer;
import com.entity.Style;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/StyleServlet")
public class StyleServlet extends BaseServlet {

	/**
	 *  查询所有歌曲风格
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		StyleService styleService = (StyleService) ProxyUtil.getDaoProxy(new StyleServiceImpl());
		List<Style> list = null;
		try {

			list =  styleService.queryData(null);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(list);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 添加一个风格
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addStyle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());
		int styleId = 0;
		try {
			String styleName = req.getParameter("name");
			if(styleName == null || styleName.length() == 0) throw new RuntimeException("请输入风格名字");

			styleId = songService.addStyle(styleName);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}
		jsonUtil.setData(styleId);
		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}


	/**
	 * 删除风格
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		StyleService styleService = (StyleService)ProxyUtil.getDaoProxy(new StyleServiceImpl());
		int res = 0;
		try {
			String styleId = req.getParameter("id");
			if(styleId == null || styleId.length() == 0) throw new RuntimeException("请选择删除的风格");

			res = styleService.deleteById(styleId);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}
//		jsonUtil.setData(styleId);
		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}
}
